<extend name="Common/main"/>

<block name="title">
    <title>师生管理</title>
</block>
<block name='css'>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/css/team.css">
</block>

<block name="content">
        <!--content-->
        <div class="col-sm-offset-2">

            <div class="panel panel-default">
                <div class="panel-body" style="height:70px;">
                    <div class="form-group">
                        <div class="col-sm-3">
                            <div class="input-group">
                                <input class="form-control" type="text" id="c_or_n" placeholder="学号或姓名">
                           <span class="input-group-btn">
                               <button class="btn btn-default">搜索</button>
                            </span>
                            </div>
                        </div>

                        <div class="col-sm-1 col-sm-offset-6">
                            <a onclick="javascript: addModal();"><button class="btn btn-primary ">添加师生</button></a>
                        </div>
                    </div>
                </div>
            </div>


            <table class="table table-bordered" id="table-route">
                <thead>
                <tr>
                    <th style="width: 25px;">&nbsp;</th>
                    <th style="width: 100px;">姓名</th>
                    <th style="width: 100px;">学号或工号</th>
                    <th style="width: 100px;">操作</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><input type="checkbox" value=""></td>
                    <td></td>
                    <td></td>
                    <td><a href=""><span>详情</span></a>&nbsp;<a href=""><span>编辑</span></a>&nbsp;<a href=""><span>删除</span></a></td>
                </tr>
                </tbody>
            </table>
            <div class="ml-9">
                <input type="checkbox" value="" id="allChecked" class="allChecked"><span class="ml-9">全选</span>
                <button class="btn btn-warning btn-xs ml-9" onclick="javascript: delUsers() ;">删除</button>
            </div>

            <ul class="pagination" id="pagination">
                <!-- pages -->
            </ul>

        </div>

        <!-- template_tr start -->
        <table id="template-list" style="display:none;">
            <tbody id="template-list-tbody">
            <tr id="template-list-tr">
                <td><input type="checkbox" value="id"></td>
                <td>name</td>
                <td>cardnum</td>
                <td>
                    <a class="lookup"><span class="btn-link">详情</span></a>
                    <a class="edit"><span class="btn-link">编辑</span></a>
                    <a class="del"><span class="btn-link">删除</span></a>
                </td>
            </tr>
            </tbody>
        </table>
        <input type="hidden" value="0" id="modalType"/>

        <div class="modal fade" id="showMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal_wrapper">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" onclick="closeModal()">
                                <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                            </button>
                            <h4 class="modal-title" id="myModalLabel_edit">校医院后台系统</h4>
                        </div>
                        <div class="modal-body">
                            <iframe src="" frameborder="0" id="usersMessage" style="width: 100%; height: 450px"></iframe>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" onclick="closeModal()">完成</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>



</block>

<block name="js">

    <script type="text/javascript">

        var fetchData = function(furl){
            if(furl==null){
                url = "{:U('Users/getUsersList')}";
            }else{
                url = furl;
            }

            $.ajax({
                url:url,
                type:'GET',
                dataType:'json',
                data:{
                    'search' :   $('#c_or_n').val()
                }, success:function(json){
                    if(json.state!='1'){
                        alert("出错了");
                        return;
                    }
                    var show = json.show;


                    if(show == "" || show.total == 0){
                        $('#table-route tbody').html('<tr><td colspan="7" align="center">哥无法为你找到此人>.<!!</td></tr>');
                        $('.pagination').html("");
                        return;
                    }else{
                        //列表渲染
                        var list = json.data;
                        var $template_list_tbody = $('#template-list tbody');
                        var $tds = $('#template-list td');
                        var template_render = '';
                        for(var i in list){
                            $tds[0].value = list[i].person_id;
                            $tds[1].innerHTML = list[i].name;
                            $tds[2].innerHTML = list[i].cardnum;
                            $('#template-list td .lookup').attr('href','javascript: var id ='+list[i].person_id+';openModal(id);');
                            $('#template-list td .del').attr('href','javascript: var id ='+list[i].person_id+';delUsers(id);');
                            $('#template-list td .edit').attr('href','javascript: var id ='+list[i].person_id+';editModal(id);');
                            $('#template-list td input').val(list[i].person_id);
                            $('#template-list td a').val(list[i].person_id);

                            template_render += $template_list_tbody.html();
                        }

                        $('#table-route tbody').html(template_render);
                    }
                    make_page(json.show,'.pagination','?');
                }
            })
        }

        fetchData();

        function delUsers(ids){
            var cd = confirm("确认删除？");
            if(cd == true){
                deleteUsers(ids);
            }else{
                return;
            }
        }

        function deleteUsers(ids){
            if(ids==null) {
                var ids = new Array();
                var selected = $('#table-route input:checkbox:checked');
                for (var i = 0; i < selected.length; i++) {
                    ids[i] = selected[i].value;
                }
            }

            $.ajax({
                url:"{:U('Users/delUsersById')}",
                type:'GET',
                dataType:'json',
                data:{
                    'ids':ids
                },success:function(json){
                    if(json==true){
                        fetchData();
                    } else{
                        alert("删除失败，请联系技术人员。");
                    }
                }
            });
        }

        $('#c_or_n').on("keyup",function(){
            fetchData();
        });

        function openModal(id){

            $('#usersMessage').attr('src',"{:U('Admin/Users/usersMessage')}?id="+id);
            $('#showMessage').modal().css({
                width: "100%",
                height: "100%"
            });
            $('#showMessage').modal({ show: true, backdrop: 'static' });

        }

        function editModal(id){
            $('#usersMessage').attr('src',"{:U('Admin/Users/editMessage')}?id="+id);
            $('#showMessage').modal().css({
                width: "100%",
                height: "100%"
            });
            $('#showMessage').modal({ show: true, backdrop: 'static' });

        }

        function addModal(){
            $('#usersMessage').attr('src',"{:U('Admin/Users/addMessage')}");
            $('#showMessage').modal().css({
                width: "100%",
                height: "100%"
            });
            $('#showMessage').modal({ show: true, backdrop: 'static' });
        }

        function closeModal(){
            $('#showMessage').modal("hide");
            fetchData();
        }
    </script>

</block>
